package com.zjs.file.operation;

import org.springframework.util.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

/**
 * 文件操作界面
 * @author zhu_jinsheng
 * @version 1.0
 * @date 2020/7/4 9:58
 */
public class FileOperationJf {

    private JFrame jFrame;
    private JPanel panel1, panel2;
    private JMenuBar menubar;
    private JMenu menu1, menu2, menu3, menu4;
    private JMenuItem item1, item2, item3, item4, item5;
    public FileOperationJf(){
        initFrame();
        frameLayout();
        bindMenu();
        bindChooseFileBtn();
        showFrame();
    }

    /**
     * 初始化窗体
     */
    public void initFrame() {
        jFrame = new JFrame();
        jFrame.setTitle("文件操作");
        jFrame.setSize(1200,900);
        jFrame.setLocation(350,120);
        jFrame.setVisible(true);
    }

    /**
     * 展示窗体
     */
    public void showFrame() {
        jFrame.setVisible(true);
    }

    /**
     * 窗体布局
     */
    public void frameLayout() {
        //四个数字依次对应 行 列 水平间距，垂直间距
//        jFrame.setLayout(new GridLayout(3,5,13,13));
        jFrame.setLayout(new FlowLayout());
        panel1 = new JPanel();
        panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 30));
        jFrame.add(panel1);
    }

    /**
     * 绑定菜单栏
     */
    public void bindMenu() {
        menubar=new JMenuBar();
        menu1=new JMenu("文件(F)");
        menu1.setMnemonic('F');
        menu2=new JMenu("编辑(E)");
        menu2.setMnemonic('F');
        menu3=new JMenu("查看(V)");
        menu3.setMnemonic('V');

        menu4=new JMenu("打开(O)");
        menu4.setMnemonic('O');

        item2=new JMenuItem("保存",new ImageIcon("save.gif"));
        item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
        item3=new JMenuItem("复制",new ImageIcon("copy.gif"));
        item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
        item4=new JMenuItem("停止",new ImageIcon("stop.gif"));
        item4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_MASK));
        item5=new JMenuItem("刷新",new ImageIcon("Snew.gif"));
        item5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));

        menu1.add(menu4);
        menu4.add(new JMenuItem("打开x",new ImageIcon("open.gif")));
        menu4.add(new JMenuItem("打开y",new ImageIcon("open.gif")));
        menu1.addSeparator();
        menu1.add(item2);
        menu2.add(item3);
        menu3.add(item4);
        menu3.add(item5);

        menubar.add(menu1);
        menubar.add(menu2);
        menubar.add(menu3);

        jFrame.setJMenuBar(menubar);
        jFrame.validate();
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * 绑定文件名称修改器
     */
    private void bindChooseFileBtn() {
        JButton chooseFileBtn= new JButton("选择");
        JButton updateBtn= new JButton("修改");
        Label pathLab = new Label("路径：未选择");

        //路径显示Label设置
        pathLab.setPreferredSize(new Dimension(500, 50));
        pathLab.setFont(new Font("宋体", Font.PLAIN, 20));

        //选择路径事件监听
        chooseFileBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser("D:\\");
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int returnVal = fileChooser.showOpenDialog(fileChooser);
                if(returnVal == JFileChooser.APPROVE_OPTION){
                    // 选择的文件夹的路径
                    String filePath = fileChooser.getSelectedFile().getAbsolutePath();
                    pathLab.setText("路径：" + filePath);
                }
            }
        });

        //修改点击事件监听
        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateFileName(pathLab.getText().substring(3).trim());
            }
        });

        panel1.add(chooseFileBtn);
        panel1.add(updateBtn);
        panel1.add(pathLab);
    }

    /**
     * 修改文件名称
     * @param filePath
     */
    private void updateFileName(String filePath) {
        if (StringUtils.isEmpty(filePath) || "未选择".equals(filePath)) {
            JOptionPane.showMessageDialog(null, "选择路径不能为空",
                    "Title",JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            ModifyFileName.modifyFileName(filePath);
            JOptionPane.showMessageDialog(null, "修改成功",
                    "Title", JOptionPane.PLAIN_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "修改失败",
                    "Title", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] args) {
        FileOperationJf win = new FileOperationJf();
    }
}
